#ifndef YANIMATION_H
#define YANIMATION_H

/*To be animated, an object must be able to be
	1: Copied
	2: Multiplied by a float
	3: Added to another object of the same type
*/

#include "AnimationBase.h"

namespace Y{

template <class C>
class Animation: public AnimationBase{
	
	public:
		
		C *animatedObject;
		C delta;
		C end;
		
	public:
	
		Animation( Entity *parentis = NULL ):
			AnimationBase(parentis),
			animatedObject(NULL)
		{
			connect( "tick", this, FUNC(Entity,update) );
		}
		
		virtual void handleConfig ( ConfigVector& cv ){
			for ( int i = 0; i < cv.size(); ++i ){
				if ( cv[i].name == "object ptr" ){ animatedObject = cv[i].to<C *>();handled(); }
				else if ( cv[i].name == "end" || cv[i].name == "end state" || cv[i].name == "end object" ){
					end = cv[i].to<C>();handled();
				}
			}
			AnimationBase::handleConfig(cv);
		}
		
		virtual void object ( C& obj ){ animatedObject = &obj; }
		virtual void object ( C* obj ){ animatedObject = obj; }
		virtual C& object () { return *animatedObject; }
		
		virtual void start (){
			C start = object();
			delta = end + (start * -1.0);
			if ( endCount == 0.0 ){ std::cout << "\n" << "bad end count: 0.0";FLUSH; }
			delta = delta * (1.0 / endCount);
			
			AnimationBase::start();
		}
		
		virtual bool tick ( float f = 1.0 ){
			if ( counter < endCount ){
				if ( counter + f < endCount ){
					*animatedObject = (*animatedObject + ( delta * f ));
					return true;
				}
				else{
					*animatedObject = end;
					return false;
				}
			}
			return false;
		}
};

}
#endif
